﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using BoundaryLightLayout;
using YArchitech.Revit;
using YJKArch.Utils;
using LightingEquipmentLay.Utility;
using TopologySearch;

namespace Command
{
	[Transaction(TransactionMode.Manual)]
	public class CmdTestLightLayout : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			try
			{
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				List<Pole2D> list = new List<Pole2D>();
				List<Curve> list2 = new List<Curve>();
				foreach (Reference reference in activeUIDocument.Selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1))
				{
					DetailCurve detailCurve = document.GetElement(reference) as DetailCurve;
					if (detailCurve != null)
					{
						list2.Add(detailCurve.GeometryCurve);
						Pole2D item = Pole2D.TransformWallToStructure2DPole(detailCurve.GeometryCurve, ElementId.InvalidElementId, 0.0, 0.0);
						list.Add(item);
					}
				}
				foreach (List<Curve> list3 in new StructureLoopSearcher(document).GetCellsEdgeLoops(list, true))
				{
					LightLayoutOper lightLayoutOper = new LightLayoutOper(document, list3, UnifiedModified.CreatePlane(XYZ.BasisZ, XYZ.Zero));
					double dRoomArea = Geometry.CurveAoArea(list3) * 304.8 * 304.8 / 1000.0 / 1000.0;
					double dIllumStandard = 30.0;
					double dIllumFlex = 2500.0;
					double dUserFactor = 0.6;
					double dLLF = 0.7;
					ushort nLightCount = LightingEquipmentLay.Utility.Common.RoomIllumCalcLampCount(dRoomArea, dIllumStandard, dIllumFlex, dUserFactor, dLLF);
					lightLayoutOper.DoLightLayout(nLightCount);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
